/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercises.day02.pt01;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author (jaice)
 * @version 0.1 (may 28, 2012)
 * 
 * 
 * a. Create a Java application which displays your name, age, and
 * birthdate (use appropriate variables).  Use the constructor to
 * initialize the fields with default values.
 * 
 */
public class NameConstructor {
    //declare variables

    private String name;
    private int age;
    private Date birthdate;

    //getters and setters
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthdate() {
        return this.birthdate;
    }

    public void setBirthdate(String birthdate) throws ParseException {


        try {
            DateFormat formatter;


            formatter = new SimpleDateFormat("dd-MMM-yy");
            this.birthdate = (Date) formatter.parse(birthdate);

        } catch (ParseException e) {
            System.out.println("Exception :" + e);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * NameConstuctor
     * 
     * @param name
     * @param age
     * @param birthdate 
     * 
     */
    public NameConstructor(String name, int age, String birthdate) {

        //convert from string to date?

        this.name = name;
        this.age = age;

        try {
            DateFormat formatter;


            formatter = new SimpleDateFormat("dd-MMM-yy");
            this.birthdate = (Date) formatter.parse(birthdate);

        } catch (ParseException e) {
            System.out.println("Exception :" + e);
        }


    }

    public NameConstructor() throws ParseException {
        this.name = "generic";
        this.age = 0;
        this.setBirthdate("01-jan-1900");

    }

    public NameConstructor(String name, String birthdate) throws ParseException {
        this.name = name;
        this.setBirthdate(birthdate);

        // Create a calendar object with today's date
        Calendar today = Calendar.getInstance();
        Calendar birth = Calendar.getInstance();
        birth.setTime(this.getBirthdate());

        int age = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);


// Add the tentative age to the date of birth to get this year's birthday
        birth.add(Calendar.YEAR, age);

// If this year's birthday has not happened yet, subtract one from age
        if (today.before(birth)) {
            age--;
        }

        this.age = age;

    }
}
